#ifndef __JSONRPC_H__
#define __JSONRPC_H__

#include <glib.h>
#include <gio/gio.h>

#include "json/json.h"

#ifdef __cplusplus
extern "C" {
#endif

#define JSONRPC_VERSION "2.0"

/*
 *
 * http://www.jsonrpc.org/specification
 *
 * code	message	meaning
 * -32700	Parse error	Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
 * -32600	Invalid Request	The JSON sent is not a valid Request object.
 * -32601	Method not found	The method does not exist / is not available.
 * -32602	Invalid params	Invalid method parameter(s).
 * -32603	Internal error	Internal JSON-RPC error.
 * -32000 to -32099	Server error	Reserved for implementation-defined server-errors.
 */

#define JSONRPC_PARSE_ERROR -32700
#define JSONRPC_MESSAGE_PARSE_ERROR "Parse error"

#define JSONRPC_INVALID_REQUEST -32600
#define JSONRPC_MESSAGE_INVALID_REQUEST "Invalid Request"

#define JSONRPC_METHOD_NOT_FOUND -32601
#define JSONRPC_MESSAGE_METHOD_NOT_FOUND "Method not found"

#define JSONRPC_INVALID_PARAMS -32602
#define JSONRPC_MESSAGE_INVALID_PARAMS "Invalid params"

#define JSONRPC_INTERNAL_ERROR -32603
#define JSONRPC_MESSAGE_INTERNAL_ERROR "Internal error"

typedef struct {
	GSocketService *service;
	GHashTable *methods;
} jsonrpc_server;

typedef json_object* (*jsonrpc_method_ptr)(json_object *);

void jsonrpc_register_method(jsonrpc_server *server, const char *name, jsonrpc_method_ptr method);

json_object *jsonrpc_invoke(jsonrpc_server *server, const char *json);

#ifdef __cplusplus
}
#endif

#endif
